const { date } = require("joi");
const db = require("../db/index");
const bcrypt = require("bcrypt");

exports.getUserinfo = (req, res) => {
  const { account } = req.query;
  const sql = "select * from users where account=?";

  db.query(sql, account, (err, result) => {
    if (err) return res.cc(err);
    if (result.length !== 1) return res.cc("用户不存在");
    res.send({
      status: 0,
      message: "获取用户信息成功",
      data: result[0],
    });
  });
};

exports.bindAccount = (req, res) => {
  const { account, uuid, url } = req.body;
  db.query("update images set account=? where uuid=?", [account, uuid], (err, result) => {
    if (err) return res.cc(err);
    if (result.affectedRows !== 1) return res.cc("绑定失败");
    const sql = "update users set avatar=? where account=?";
    db.query(sql, [url, account], (err, result) => {
      if (err) return res.cc(err);
      res.send({
        code: 0,
        message: "修改成功",
        data: {
          account: account,
          url: url,
        },
      });
    });
  });
};

exports.changeNickname = (req, res) => {
  const { account, nickname } = req.body;
  db.query("update users set nickname=? where account=?", [nickname, account], (err, result) => {
    if (err) return res.cc(err);
    if (result.affectedRows != 1) {
      return res.cc("账号昵称修改失败");
    }
    res.send({
      code: 0,
      message: "修改成功",
      data: {
        account: account,
        nickname: nickname,
      },
    });
  });
};

exports.changeSex = (req, res) => {
  const { account, sex } = req.body;
  db.query("update users set sex=? where account=?", [sex, account], (err, result) => {
    if (err) return res.cc(err);
    if (result.affectedRows != 1) {
      return res.cc("账号性别修改失败");
    }
    res.send({
      code: 0,
      message: "修改成功",
      data: {
        account: account,
        sex: sex,
      },
    });
  });
};

exports.changePassword = (req, res) => {
  const { account, oldPassword, newPassword } = req.body;
  db.query("select password from users where account = ?", account, (err, result) => {
    if (err) return res.cc(err);
    if (result.length != 1) {
      return res.cc("账号异常");
    }
    const compareResult = bcrypt.compareSync(oldPassword, result[0].password);
    if (!compareResult) {
      return res.cc("旧密码不正确,请重新输入");
    }
    const password = bcrypt.hashSync(newPassword, 16);
    db.query("update users set password=? where account=?", [password, account], (err, result) => {
      if (err) return res.cc(err);
      if (result.affectedRows != 1) {
        return res.cc("密码修改失败");
      }
      res.send({
        code: 0,
        message: "修改成功",
        data: {
          account: account,
        },
      });
    });
  });
};

exports.changeEmail = (req, res) => {
  const { account, email } = req.body;
  db.query("update users set email=? where account=?", [email, account], (err, result) => {
    if (err) return res.cc(err);
    if (result.affectedRows != 1) {
      return res.cc({ msg: "账号邮箱修改失败", code: 1 });
    }
    res.send({
      code: 0,
      message: "修改成功",
      data: {
        account: account,
        email,
      },
    });
  });
};
